package com.sharemarking.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.sharemarking.wa.common.entity.Member;
import com.sharemarking.system.dto.Page;
import com.sharemarking.system.mapper.MemberMapper;
@Controller
@RequestMapping("system/member")
public class MemberController {
	
	@Autowired
	private MemberMapper mm;
	@Autowired
	private HttpSession session;
	/**
	 * 用户列表页面
	 * @param search
	 * @param model
	 */
	@GetMapping("index")
	@PreAuthorize("hasPermission('base','all')")
	public String index(Member member,Integer page,Model model,Integer isSession){
		int size=10;
		if(page==null)page=0;
		// session 参数回显
		if (isSession != null) {
			// 如果isSession为1，代表要从session取条件参数，一般应用在某操作之后要保存查询参数
			member = session.getAttribute("query") == null ? member : (Member) session.getAttribute("query");
			size = session.getAttribute("size") == null ? size : (int) session.getAttribute("size");
			page = session.getAttribute("page") == null ? page : (int) session.getAttribute("page");
		}
		Map<String,Object> map = new HashMap<>();
		map.put("pageIndex",page*size);
		map.put("pageSize",size);
		if(!StringUtils.isEmpty(member.getMobile())) {
			map.put("mobile",member.getMobile());
		}	
		List<Member> list=mm.getPage(map);
		for(Member obj:list) {
			obj.setMobile(obj.getMobile().substring(0,obj.getMobile().length()-4)+"****");
		}
		int total = mm.getPageTotal(map);
		model.addAttribute("page", new Page<>(page, size, total, list));
		model.addAttribute("query",member);
		// session 参数保存
		session.setAttribute("query", member);
		session.setAttribute("size", size);
		session.setAttribute("page", page);
		return "member/index";
	}
}
